package com.lon.over.ui.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.lon.over.model.domain.HomePagerContent;
import com.lon.over.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: ctl | 陈泰龙
 * 创建时间: 2022/4/25
 * Describe:
 */
public class LopperPagerAdapter extends PagerAdapter {

    private List<HomePagerContent.DataBean> mData = new ArrayList<>();


    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        // 处理越界问题
        int resultPosition = position % mData.size();
        HomePagerContent.DataBean dataBean = mData.get(resultPosition);
        int height = container.getMeasuredHeight();
        int width = container.getMeasuredWidth();
        int coverSize = (Math.max(width, height)) / 2;
        String coverUrl = UrlUtils.getPathCover(dataBean.getPict_url(), coverSize);
        ImageView img = new ImageView(container.getContext());
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        img.setLayoutParams(layoutParams);
        img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(container.getContext()).load(coverUrl).into(img);
        container.addView(img);
        return img;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    public void setData(List<HomePagerContent.DataBean> content) {
        mData.clear();
        mData.addAll(content);
        notifyDataSetChanged();
    }

    public int getDataSize() {
        return mData.size();
    }

}
